<template>
  <div>
    <BreakSpace label="物流信息" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel ref="FormPanel" :list="formList" labelWidth="100" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle2" />
    <div :style="{  textAlign: 'center'  }" >
      <el-button @click="saveDialog" type="primary" :loading="saveLoading" v-if="type === 'edit'">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { updatePartPrice,compareWsName,storequeryall} from '@/api/partsmanage/carrierTraceRepair/query';
import {phone,banWord} from "@/utils/validate";
export default {
  name: 'carrierTrace',
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      activeName: 'first',
      saveLoading: false,
      wsNameList:[],
      TranceInfo:[],
      fetchParams: {
        id: ''
      }
    };
  },
  created() {
    this.fetchParams.id = this.rowData.id;
    this.init();
  },
  mounted() {
    this.getGroupInfo();
  },
  methods: {
    init() {
      this.queryAllWsName()
      if (this.rowData.type === 'info'){
        this.formList.forEach(x => {
          if (x.fieldName === 'fyDate' || x.fieldName === 'remark' || x.fieldName === 'wsPhone'||x.fieldName === 'wsCode'|| x.fieldName === 'wsPerson'|| x.fieldName === 'planJhDate'|| x.fieldName === 'traceNo'|| x.fieldName === 'wsName'){
            x.disabled = true
          }
          x.initialValue = this.rowData[x.fieldName]
        })
      }else{
        this.type='edit';
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'companyCode',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '服务商名称',
          fieldName: 'companyName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件订单号',
          fieldName: 'cgNo',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'SAP订单号',
          fieldName: 'sapNo',
          hidden: true
        },
        {
          type: 'INPUT',
          label: '交货单号',
          fieldName: 'jhNo',
          disabled: true
        },
        {
          type: 'DATE',
          label: '发运日期',
          fieldName: 'fyDate',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {required: true,message: '请输入发运日期',trigger: 'change'}
          ],
        },
        // {
        //   type: 'INPUT',
        //   label: '物流公司',
        //   fieldName: 'wsName',
        //   rules: [{ max: 20, message: '长度在20个字符以内', trigger: 'blur' }],
        // },
        {
          type: 'DATE',
          label: '预计到货时间',
          fieldName: 'planJhDate',
          valueFormat: 'yyyy-MM-dd',
          // rules: [{required: true,message: '请输入预计到货时间',  trigger: "blur"}],
        },
        {
          type: 'SELECT',
          label: '物流商名称',
          fieldName: 'wsCode',
          itemList: [],
          rules: [{required: true,message: '请输入物流商名称',  trigger: "blur"}],
          change: val => this.handleInputChange(val)
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'wsPerson',
          // rules: [{ max: 20, message: '长度在20个字符以内', trigger: 'blur' }],
          initialValue:''
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'wsPhone',
          // rules: [{required: true,message: '请输入电话号码',  trigger: "blur"},{ validator: phone, trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '物流单号',
          fieldName: 'traceNo',
          rules: [{ validator: banWord, trigger: 'blur' },{required: true,message: '请输入物流单号',  trigger: "blur"}],
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 2,
          maxlength: 200,
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle2(val) {},
    async queryAllWsName() {
      let res = await compareWsName();
      var li=JSON.stringify(res,["data"]);
      var kds=JSON.parse(li);
      this.wsNameList=kds.data;
    },
    async saveDialog() {
      var formParams = this.$refs.FormPanel.SUBMIT_FORM();
      if (formParams === null) {
        return false;
      }
      // if(formParams.planJhDate===""){
      //   return this.$message.warning("请维护预计到货时间");
      // }
      // if(new Date().getTime() >=new Date(formParams.planJhDate).getTime() + 3600 * 1000 * 24){
      //   return this.$message.warning("预计到货时间不能小于当前时间");
      // }

      // if(formParams.planJhDate ==  this.rowData.planJhDate){
      //   return this.$message.warning("请更新到货时间数据！");
      // }
      // if(formParams.wsPerson ===""){
      //   return this.$message.warning("请填写联系人名称！");
      // }
      // if(formParams.traceNo ===""){
      //   return this.$message.warning("请维护物流单号！");
      // }
      if(formParams.fyDate ===""){
        return this.$message.warning("请填写发运日期！");
      }
      if(formParams.wsPerson ==="null"){
        return this.$message.warning("请不要输入无意义联系人名称！");
      }
      if(formParams.wsCode===""){
        return this.$message.warning('请选择物流商！');
      }
      if(!this.wsNameList.includes(formParams.wsCode)){
       return this.$message.warning('输入的物流商不存在，请重新输入！');
      }
      this.saveLoading = true;
      let saveinfos = {};
        saveinfos.id=this.rowData.id,
        saveinfos.tremark= formParams.remark,
        saveinfos.tpwsId= this.rowData.tpwsId,
        saveinfos.twsId= this.rowData.twsId,
        saveinfos.tpwsFyDate=formParams.fyDate,
        saveinfos.tpwsPlanJhDate=formParams.planJhDate,
        saveinfos.twsWsPerson= formParams.wsPerson,
        saveinfos.twsWsPhone= formParams.wsPhone,
        saveinfos.twsWsName= formParams.wsCode,
        saveinfos.twstraceNo=formParams.traceNo,
        saveinfos.recordVersion=this.rowData.recordVersion,
        saveinfos.jhNo=this.rowData.jhNo,
        saveinfos.cgNo=this.rowData.cgNo,
        saveinfos.xstjdz=this.rowData.xsdjzt
      const res = await updatePartPrice(saveinfos);
      if (res.resultCode === 200) {
        this.$message({
          type: 'info',
          message: '保存成功'
        });
        this.closeDialog('refresh');
      }
      this.saveLoading = false;
    },
    async getGroupInfo() {
      const res = await storequeryall();
      this.TranceInfo=res.data;
      this.$nextTick(() => {
        this.formList.find(item => item.fieldName === 'wsCode').itemList = res.data.map(x => ({
          value: x.wsCode + '',
          text: x.wsName
        }));
      })
    },
    async handleInputChange(val) {
      this.formList.find(item => item.fieldName === 'wsPerson').initialValue =this.TranceInfo.filter(x => x.wsCode === val).map(x => x.wsPerson );
      this.formList.find(item => item.fieldName === 'wsPhone').initialValue =this.TranceInfo.filter(x => x.wsCode === val).map(x => x.wsPhone );
      // this.formList.find(item => item.fieldName === 'remark').initialValue =this.TranceInfo.filter(x => x.wsCode === val).map(x => x.remark );
      this.formList.find(item => item.fieldName === 'wsPhone').disabled = true;
      this.formList.find(item => item.fieldName === 'wsPerson').disabled = true;
      return;
    },
    closeDialog(param) {
      this.$emit('close', { visible: false, type: param });
    }
  }
};
</script>

<style scoped></style>
